﻿using System;
using l1cs.server.datatables;
using l1cs.server.model;
using l1cs.server.model.Instance;
using l1cs.server.model.npc;
using l1cs.server.serverpackets;
using l1cs.server.templates;
using l1cs.server.utils;
using l1cs.server.model.npc.action;

namespace l1cs.server.clientpackets
{
    public class C_Amount : ClientBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( C_Amount ).FullName );
        private const String _C_AMOUNT = "[C] C_Amount";

        public C_Amount( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {

            int objectId = readD();
            int amount = readD();
            int c = readC();
            String s = readS();

            L1PcInstance pc = client.getActiveChar();
            L1NpcInstance npc = (L1NpcInstance)L1World.getInstance().findObject( objectId );
            if ( npc == null )
            {
                return;
            }

            String s1 = "";
            String s2 = "";
            StringTokenizer stringtokenizer = new StringTokenizer( s );
            s1 = stringtokenizer.nextToken();
            s2 = stringtokenizer.nextToken();
            //equalsIgnoreCase
            if ( string.Compare( s1, "agapply", true ) == 0 )
            { // 競売に入札した場合
                String pcName = pc.getName();
                AuctionBoardTable boardTable = new AuctionBoardTable();
                foreach ( L1AuctionBoard board in boardTable.getAuctionBoardTableList() )
                {
                    if ( string.Compare( pcName, board.getBidder(), true ) == 0 )
                    {
                        pc.sendPackets( new S_ServerMessage( 523 ) ); // すでに他の家の競売に参加しています。
                        return;
                    }
                }
                int houseId = Convert.ToInt32( s2 );
                L1AuctionBoard l1board = boardTable.getAuctionBoardTable( houseId );
                if ( l1board != null )
                {
                    int nowPrice = l1board.getPrice();
                    int nowBidderId = l1board.getBidderId();
                    if ( pc.getInventory().consumeItem( L1ItemId.ADENA, amount ) )
                    {
                        // 競売掲示板を更新
                        l1board.setPrice( amount );
                        l1board.setBidder( pcName );
                        l1board.setBidderId( pc.getId() );
                        boardTable.updateAuctionBoard( l1board );
                        if ( nowBidderId != 0 )
                        {
                            // 入札者にアデナを返金
                            L1PcInstance bidPc = (L1PcInstance)L1World.getInstance().findObject( nowBidderId );
                            if ( bidPc != null )
                            { // オンライン中
                                bidPc.getInventory().storeItem( L1ItemId.ADENA, nowPrice );
                                // あなたが提示された金額よりももっと高い金額を提示した方が現れたため、残念ながら入札に失敗しました。%n
                                // あなたが競売に預けた%0アデナをお返しします。%nありがとうございました。%n%n
                                bidPc.sendPackets( new S_ServerMessage( 525, Convert.ToString( nowPrice ) ) );
                            }
                            else
                            { // オフライン中
                                L1ItemInstance item = ItemTable.getInstance()
                                        .createItem( L1ItemId.ADENA );
                                item.setCount( nowPrice );
                                CharacterItemsTable.storeItem( nowBidderId, item );
                            }
                        }
                    }
                    else
                    {
                        pc.sendPackets( new S_ServerMessage( 189 ) ); // \f1アデナが不足しています。
                    }
                }
            }
            else if ( string.Compare( s1, "agsell", true ) == 0 )
            { // 家を売った場合
                int houseId = Convert.ToInt32( s2 );
                AuctionBoardTable boardTable = new AuctionBoardTable();
                L1AuctionBoard board = new L1AuctionBoard();
                if ( board != null )
                {
                    // 競売掲示板に新規書き込み
                    board.setHouseId( houseId );
                    L1House house = HouseTable.getInstance().getHouseTable( houseId );
                    board.setHouseName( house.getHouseName() );
                    board.setHouseArea( house.getHouseArea() );
                    board.setDeadline( DateTime.Today.AddDays( 5 ) );
                    board.setPrice( amount );
                    board.setLocation( house.getLocation() );
                    board.setOldOwner( pc.getName() );
                    board.setOldOwnerId( pc.getId() );
                    board.setBidder( "" );
                    board.setBidderId( 0 );
                    boardTable.insertAuctionBoard( board );

                    house.setOnSale( true ); // 競売中に設定
                    house.setPurchaseBasement( true ); // 地下アジト未購入に設定
                    HouseTable.getInstance().updateHouse( house ); // DBに書き込み
                }
            }
            else
            {
                L1NpcAction action = NpcActionTable.getInstance().get( s, pc, npc );
                if ( action != null )
                {
                    L1NpcHtml result = action.executeWithAmount( s, pc, npc, amount );
                    if ( result != null )
                    {
                        pc.sendPackets( new S_NPCTalkReturn( npc.getId(), result ) );
                    }
                    return;
                }
            }
        }

        override
        public String getType()
        {
            return _C_AMOUNT;
        }
    }
}